package com.ljb.immersiveutils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

/**
 * 设置TitleBar为沉浸模式的工具类
 * Created by lijianbin on 2016/12/9.
 */
public class ImmersiveUtils {
    public static ImmersiveUtils mUtils;
    private View status;

    public static ImmersiveUtils initUtils() {
        if (mUtils == null)
            mUtils = new ImmersiveUtils();
        return mUtils;
    }

    /**
     * 设置状态栏在4.4版本以上显示半透明沉浸模式
     *
     * @param status    充当状态栏的view
     * @param color     状态栏显示的颜色
     * @param mActivity Activity
     */
    public void initImmersive(View status, String color, Activity mActivity) {
        this.status = status;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            initStatusView(mActivity, color);
        }
    }

    private View initStatusView(Context mContext, String color) {
        status.setBackgroundColor(Color.parseColor(color));
        status.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(mContext)));
        return status;
    }

    private int getStatusBarHeight(Context mContext) {
        int result = 0;
        int resourceId = mContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = mContext.getResources().getDimensionPixelSize(resourceId);
        }
        Log.e("StatusBarHeight:", "状态栏的高度为:" + result);
        return result;
    }
}
